﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Quartz;
using ServiceContainer.Commom;
using YouWei.Infrastructure.Entity;
using YouWei.Infrastructure.Enums;
using YouWei.Infrastructure.Service;
using YouWei.Utility;
using YouWei.Utility.Logging;

namespace ServiceContainer.Jobs
{
    /// <summary>
    /// 短信接收信息任务
    /// </summary>
    [DisallowConcurrentExecution]
    public class SmsReceiveJob : IJob
    {
        JobEnum jobEnum = JobEnum.ReceiveJob;//作业ID
        public void Execute(IJobExecutionContext context)
        {
            try
            {
                while (true)
                {
                    DoWork();
                    Thread.Sleep(10*60*1000);   //10分钟获取一次信信回复信息
                }
                
            }
            catch (Exception ex)
            {
                Log4netLog.Error(101,context.JobDetail.Key + "作业出现异常,异常信息为：" + ex);
            }
        }

        public void DoWork()
        {
            var svReceive=new SmsReceiveService();
            var svDatasource=new sms_recvService();
            var svMessagePool=new SmsMessagePoolService();
            var svJobRecord = new JobRecordService();
            var svTpl=new SmsTemplateService();
            var lastDataRecordTime = DateTime.Now.AddYears(-1);
            //获取最后一次执行作业的时间
            var oLastJobRecordInfo = svJobRecord.GetLastExcuteJobRecordInfo(jobEnum);
            if (oLastJobRecordInfo != null && !oLastJobRecordInfo.LastDataRecordTime.IsNullDateTime())
            {
                lastDataRecordTime = oLastJobRecordInfo.LastDataRecordTime;
            }
            else
            {
                oLastJobRecordInfo = new JobRecord();
            }
            var lastTimeText = lastDataRecordTime.ToString("yyyy-MM-dd HH:mm:ss");

            var listRece = svDatasource.GetNeedToSyncMsgs(lastTimeText, 50);
            var szCarBrand = "";
            var szCarSerial = "";
            for (var i=0;i<listRece.Count;i++)
            {
                var item = listRece[i];
                var oReceive=new SmsReceive();
                var possbileSendTime = EConvert.ToDateTime(item.smsDate).AddDays(-15);
              
                var oMsg = svMessagePool.QuerySingle(m => m.ReceiveMobile == item.smsNumber && m.SendDate>possbileSendTime);
                if (oMsg != null)
                {
                    //var oTpl = svTpl.QuerySingle(m => m.TemplateID == oMsg.SmsTplID) ?? new SmsTemplate();
                    //szCarBrand = oTpl.CarBrad;
                    //szCarSerial = oTpl.CarSerial;
                    //if (string.IsNullOrEmpty(szCarBrand))
                    //{
                    //    szCarBrand = oMsg.CarBrand;
                    //    szCarSerial = oMsg.CarSerial;
                    //}
                    szCarBrand = oMsg.CarBrand;
                    szCarSerial = oMsg.CarSerial;

                    oReceive.CarBrand = szCarBrand;
                    oReceive.CarSerial = szCarSerial;
                    oReceive.SendNum = oMsg.SendMobile;
                    oReceive.SendContent = oMsg.Content;
                    oReceive.SendTime = oMsg.SendDate;
                    oReceive.ReplyNum = item.smsNumber;
                    oReceive.ReplyContent = item.smsContent;
                    oReceive.ReplyTime = EConvert.ToDateTime(item.smsDate);
                    oReceive.CreatedTime = DateTime.Now;
                    svReceive.Insert(oReceive);
                }
                oLastJobRecordInfo.LastDataRecordTime = EConvert.ToDateTime(item.smsDate);
            }
               
            oLastJobRecordInfo.CreatedDate = DateTime.Now;
            oLastJobRecordInfo.JobId = (int)jobEnum;
            svJobRecord.Insert(oLastJobRecordInfo);

        }
    }
}
